<template>
  <div class="container">
    <el-form :model="param">
      <div class="form_box">
        <el-form-item class="account" label="广告标题">
          <el-input v-model="param.title" placeholder="请输入广告标题"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="param.status" placeholder="请选择状态">
            <el-option label="禁用" :value="0"></el-option>
            <el-option label="启用" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </div>
      <el-form-item class="btn_box">
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
        <el-button type="primary" @click="addAD">添加</el-button>
      </el-form-item>
    </el-form>
    <div class="auth_box">
      <el-button @click="delAllFiles" type="info">删除</el-button>
    </div>
    <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" align="center"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="title" label="广告标题" width="300" show-overflow-tooltip></el-table-column>
      <el-table-column label="广告位置" column-key="ad_category" :formatter="getName" prop="category" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="类型" column-key="ad_type" :formatter="getName" prop="type"></el-table-column>
      <el-table-column label="开始时间" prop="startTime"></el-table-column>
      <el-table-column label="到期时间" prop="endTime"></el-table-column>
      <el-table-column label="状态" width="60">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" @change="updateStatus(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="orderNo" label="排序" width="60"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="updateAD(scope.row)" type="text" size="small">修改</el-button>
          <el-button type="text" size="small" @click="delFile(scope.row)" class="red">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="param.page" :page-sizes="[10,30,60,100,200,300]" :page-size="param.size"
        layout="prev, pager, next, jumper, sizes, total" :total="total"></el-pagination>
    </div>
  </div>
</template>

<script>
import { ADList, delAD, delAllfiles, updatestatus, getADtype, getADPosition } from '../../../api/content';
import { postPicture } from '../../../api/index';
export default {
  name: 'Advertisement',
  data () {
    return {
      total: 0,
      param: {
        page: 1,
        size: 10,
        title: '',
        status: null
      },
      selection: [],
      options: {},
      tableData: []
    };
  },
  created () {
    let obj = {
      ad_type: getADtype,
      ad_category: getADPosition
    };
    Promise.all(Object.values(obj).map(item => item())).then(data => {
      Object.keys(obj).forEach((key, index) => {
        this.options[key] = data[index].data;
      });
      console.log(this.options);
      this.getADList();
    });
  },
  methods: {
    getDic () { },
    search () {
      this.param.page = 1;
      this.getADList();
    },
    // 获取广告列表
    getADList () {
      ADList(this.param).then(data => {
        console.log(data);
        if (data.success) {
          this.tableData = data.data.records;
          this.total = data.data.total;
        } else {
          this.$message.error(`${data.msg}`);
        }
      });
    },
    getName (row, column, val) {
      console.log(typeof val);

      if (typeof val !== 'number') {
        return '123';
      }
      let key = column.columnKey;
      let obj = this.options[key].find(item => item.val === val);
      if (obj) {
        return obj.name;
      } else {
        return '456';
      }
    },
    reset () {
      this.param.title = '';
      this.param.status = '';
      this.param.page = 1;
      this.param.size = 10;
      this.getADList();
    },
    handleSelectionChange (selected) {
      this.selection = selected;
    },
    updateAD (row) {
      this.$router.push({ path: '/addAdvertisement', query: { id: row.id } });
    },
    handleSizeChange (val) {
      this.param.size = val;
      this.getADList();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange (val) {
      this.param.page = val;
      this.getADList();
      console.log(`当前页: ${val}`);
    },
    searchAD () {
      this.getADList();
    },
    addAD () {
      this.$router.push({ path: '/addAdvertisement' });
    },
    delAllFiles () {
      if (this.selection.length < 1) {
        this.$message.error('请选择想要删除的广告!');
      } else {
        this.$confirm('您要删除广告，删除后不可恢复，是否确定操作？', '信息提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          callback: action => {
            if (action === 'confirm') {
              let formData = new FormData();
              formData.append('ads', this.selection.map(item => item.id).join(','))
              delAllfiles(formData).then(data => {
                console.log(data);
                if (data.success) {
                  this.$message.success('删除成功');
                  this.getADList();
                } else {
                  this.$message.error(`${data.msg}`);
                }
              });
            }
          }
        });
      }
    },
    delFile (row) {
      this.$confirm('您要删除广告，删除后不可恢复，是否确定操作？', '信息提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        callback: action => {
          if (action === 'confirm') {
            let formData = new FormData();
            formData.append('id', row.id)
            delAD(formData).then(data => {
              console.log(data);
              if (data.success) {
                this.getADList();
                this.$message.success('删除成功');
              } else {
                this.$message.error(`${data.msg}`);
              }
            });
          }
        }
      });
    },
    updateStatus (row) {
      console.log(row.status);
      updatestatus({ id: row.id, status: row.status }).then(data => {
        if (!data.success) {
          this.$message.error(`${data.msg}`);
          this.getADList();
        }
      });
    }
  }
};
</script>

<style scoped>
.companyList {
    height: 100%;
    width: 1345px;
    margin: 0 auto;
    background: #fff;
    padding: 30px 0;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -ms-box-sizing: border-box;
    -o-box-sizing: border-box;
}
.el-form .el-form-item /deep/ .el-form-item__error {
    left: 150px;
}
.el-form {
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.el-form .create_time {
    width: 212px !important;
}
.el-form .create_time /deep/ .el-date-editor {
    width: 144px;
}
.el-form .create_time /deep/ .el-date-editor /deep/ .el-input__inner {
    width: 144px;
}
.el-form .create_time /deep/ .el-form-item__content {
    display: inline-block;
}
.form_box {
    margin-left: 50px;
}
.el-form .el-form-item {
    width: 300px;
    display: inline-block;
    margin-left: 20px;
}
.el-input {
    width: 200px;
}
.el-form .account {
    width: 350px;
}
.el-form .btn_box {
    width: 400px;
    text-align: right;
}
.el-form .btn_box .el-button {
    width: 85px;
}
.el-table {
    height: 82%;
    font-size: 14px;
}
.el-table /deep/ td,
.el-table /deep/ th {
    text-align: center;
}
.red {
    color: #fd4e40;
}

.auth_box {
    margin-bottom: 20px;
    text-align: right;
}
.auth_box .el-button {
    width: 100px;
}
.page {
    width: 100%;
    height: 40px;
    background: #ffffff;
    text-align: center;
    padding-top: 20px;
}
.companyList /deep/ .el-table__body-wrapper {
    height: 655px;
}
</style>